package com.shd.edanpei.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jaeger.library.StatusBarUtil;
import com.shd.edanpei.R;
import com.shd.edanpei.base.dialog.DialogUtil;
import com.shd.edanpei.ui.adapter.ShareOrderAdapter;
import com.shd.edanpei.base.BaseFragment;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.base.rxbus.Subscribe;
import com.shd.edanpei.bean.StatusBean;
import com.shd.edanpei.presenter.contract.ShareOrderContract;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.enums.RoleEnum;
import com.shd.edanpei.presenter.ShareOrderPresenter;
import com.shd.edanpei.ui.activity.TakeOrderDetailsActivity;
import com.shd.edanpei.util.ToastUtil;
import com.shd.edanpei.view.RefreshLayout;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/9/18.
 * 共享订单
 */

public class ShareOrderFragment extends BaseFragment<ShareOrderPresenter> implements ShareOrderContract.ShareOrderIView {

    @BindView(R.id.refresh_layout)
    RefreshLayout refreshLayout;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private ShareOrderAdapter mAdapter;
    private Bundle bundle = new Bundle();
    private int roleType;

    @Override
    protected Object setLayout() {
        return R.layout.fragment_shareorder;
    }

    @Override
    protected ShareOrderPresenter bindPresenter() {
        return new ShareOrderPresenter(this);
    }

    @Override
    protected void initView(@Nullable View view, @Nullable Bundle savedInstanceState) {
        RxBus.get().register(this);
        mAdapter = new ShareOrderAdapter(getContext());
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(mAdapter);
        refreshLayout.setRefreshing(true);
        roleType = DaoManager.getInstance().getUserBean().getRoleType();
        if (roleType == RoleEnum.bank.getId()) {
            mPresenter.refresh();
        } else if (roleType == RoleEnum.other.getId()
                || roleType == RoleEnum.boss.getId()) {
            mPresenter.allRefresh();
        }
    }

    @Override
    protected void initListener() {
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (roleType == RoleEnum.bank.getId()) {
                    mPresenter.refresh();
                } else if (roleType == RoleEnum.other.getId()
                        || roleType == RoleEnum.boss.getId()) {
                    mPresenter.allRefresh();
                }
            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                bundle.putInt(Constant.ORDER_ID, mAdapter.getData().get(position).getOrderId());
                bundle.putInt(Constant.INDEX, position);
                startActivity(TakeOrderDetailsActivity.class, bundle);
            }
        });
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_take:
                        if (mAdapter.getItem(position).getUserStatus() == 2) {
                            mPresenter.take(mAdapter.getData().get(position).getOrderId(), position);
                        } else {
                            //已经被接单
                            ToastUtil.showToast("已被接单");
                        }
                        break;
                }
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if (roleType == RoleEnum.bank.getId()) {
                    mPresenter.loadMore();
                } else if (roleType == RoleEnum.other.getId()
                        || roleType == RoleEnum.boss.getId()) {
                    mPresenter.allLoadMore();
                }
            }
        }, recyclerView);
    }

//    @OnClick(R.id.headline)
//    void test(){
//        DialogUtil.showTakeSuccessDialog(getContext(), new DialogUtil.TwoListener() {
//
//            @Override
//            public void cancel() {
//
//            }
//
//            @Override
//            public void sure() {
//                RxBus.get().send(RxBusCode.CHECK_SHARE_ORDER);
//            }
//        });
//    }

    @Override
    public void onResume() {
        super.onResume();
        StatusBarUtil.setLightMode(getActivity());
    }

    @Override
    public ShareOrderAdapter getAdapter() {
        return mAdapter;
    }

    @Override
    public RefreshLayout getRefreshLayout() {
        return refreshLayout;
    }

    @Subscribe(code = RxBusCode.TAKE_ORDER_SUCCESS)
    public void takeOrderSuccess(StatusBean bean) {
        if (mAdapter.getData().size() > 0) {
            mAdapter.remove(bean.getIndex());
        }
    }

    @Override
    public void onDestroy() {
        RxBus.get().unRegister(this);
        super.onDestroy();
    }
}
